import {defineStore} from "pinia"
import router from '../router/index'
interface Itab {
    name: string;
    id: number;
    path: string;
    isChecked: boolean;
    router: Array<string>
}
const todoList = localStorage.getItem("todoList")?JSON.parse(localStorage.getItem("todoList") as string):[
    {
    id: 1,
    title: "快把你要做的事情记录下来吧",
    done: false
  }
]
export const tabStore = defineStore({
    id: "TabStore",
    state() {
        return {
            titleList: [{
                title: "message.home",
                path: "/admin"
            }],
            tabList: [{
                id: 1,
                path: "/admin",
                name: "message.home",
                isChecked: true,
                router: [
                    {title: "message.home",path: "/admin"}
                ]
            }],
            todoList
        }
    }, 
    actions: {
        // 切换tab标签
        checkTab(id: number) {
            this.tabList.forEach((e: any) => e.isChecked = false)
            let selectObj: any = this.tabList.find((e: any) => e.id == id)
            selectObj.isChecked = true
            this.titleList = selectObj.router
            router.push(selectObj.path)
        },
        // 删除tab标签
        deleteTab(tabObj:any) {
            let index: number = this.tabList.findIndex(e => e.id === tabObj.id)
            this.tabList.splice(index,1)
            let len:number = this.tabList.length
            if (tabObj?.isChecked) {
                console.log(this.tabList[len -1])
                this.tabList[len - 1].isChecked = true
                this.titleList = this.tabList[len -1].router
                setTimeout(() => {router.push(this.tabList[len - 1].path),10})
            }
        },
        // 添加标签
        addTab(tabObj: Itab) {
            // 首页判断是否存在标签
            let is_existed:boolean = this.tabList.some((e:any) => e.id == tabObj.id)
            this.tabList.forEach((e: any) => e.isChecked = false)
            if (is_existed) {
                // 存在
                let obj = this.tabList.find(item => item.id == tabObj.id)
                //@ts-ignore
                obj.isChecked = true
                this.titleList = tabObj.router as any
            } else { 
                tabObj.isChecked = true
                this.tabList.push(tabObj as any)
                this.titleList = tabObj.router as any
            }
        },
        // 添加记录
        addRecord(record:{id:number,title: string,done:boolean}) {
            console.log(this.todoList,this.showTodoList)
          this.todoList.push(record)
          localStorage.setItem('todoList',JSON.stringify(this.todoList))
        },
        // 修改记录的状态 0 => 未完成 1 => 完成
        updateRecord(id:number,done:boolean) {
            todoList.forEach((item:any) => {
                if (item.id == id) {
                    item.done = done
                }
            })
            localStorage.setItem('todoList',JSON.stringify(this.todoList))
        },
        // 删除记录
        deleteRecord(id:number) {
            this.todoList.splice(id,1)
            localStorage.setItem("todoList",JSON.stringify(this.todoList))
        }
        
    },
    getters: {
        // 获取已经完成的记录
        getDoneTodoList(state: any) {
            return state.todoList.filter((item:any) => item.done)
        },
        // 获取未完成的记录
        getUnDoneTodoList(state: any) {
            return state.todoList.filter((item:any) => !item.done)
        },
        getAllTodoList(state: any) {
            return state.todoList
        }
    }
})